package cc.comac.mom.controller;

import java.io.File;
import java.io.FileInputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cc.comac.mom.api.QualityAttachmentService;
import cc.comac.mom.api.QualityQarBasicStandardCheckService;
import cc.comac.mom.bean.QualityAttachment;
import cc.comac.mom.bean.QualityQarBasicStandardCheck;
import cc.comac.mom.entity.QualityAttachmentEntity;
import cc.comac.mom.entity.QualityQarBasicStandardCheckEntity;
import cc.comac.mom.util.ResultBean;
import cc.comac.mom.util.UploadPreviewData;

@RestController
@RequestMapping("/QualityQarBasicStandardCheck")
public class QualityQarBasicStandardCheckController {

	@Autowired
	private QualityQarBasicStandardCheckService qualityQarBasicStandardCheckService;
	
	
	@Autowired
	private QualityAttachmentService qualityAttachmentService;
	
	@RequestMapping(value = "/uploadQarBasicStandardCheck",method = RequestMethod.POST)
	public ResultBean uploadQarBasicStandardCheck(@RequestPart MultipartFile[] files) {
		ResultBean uploadQarBasicStandardCheck = qualityQarBasicStandardCheckService.uploadQarBasicStandardCheck(files);
		return uploadQarBasicStandardCheck;
	}
	
	@RequestMapping(value = "/downlaod",method = RequestMethod.POST)
	public ResultBean downlaod(HttpServletResponse response,@RequestBody QualityQarBasicStandardCheck qualityQarBasicStandardCheck) {
		ResultBean resultBean = new ResultBean(true);
		long attachmentId = qualityQarBasicStandardCheck.getInnerData().getAttachmentId();
		if (attachmentId == 0) {
			resultBean.setErrorMessge("无法获取附件主键");
			return resultBean;
		}
//		QualityAttachment selectByAttachmentId = qualityAttachmentService.selectByAttachmentId(attachmentId);
		QualityAttachmentEntity innerData = qualityAttachmentService.getByAttachmentId(attachmentId);
//		QualityAttachmentEntity innerData = selectByAttachmentId.getInnerData();
		
		
		String corrigendumName = innerData.getRealName();
		String corrigendumPath = innerData.getFilePath();
		UploadPreviewData.download(corrigendumPath, corrigendumName, response);
		return resultBean;
	}
	
	@RequestMapping(value = "/previewPdf",method = RequestMethod.POST)
	public ResultBean previewPdf( HttpServletResponse response, @RequestParam QualityQarBasicStandardCheck qualityQarBasicStandardCheck) {
		ResultBean resultBean = new ResultBean(true, ' ');
		long attachmentId = qualityQarBasicStandardCheck.getInnerData().getAttachmentId();
		if (attachmentId == 0) {
			resultBean.setErrorMessge("无法获取附件主键");
			return resultBean;
		}
		QualityAttachmentEntity innerData = qualityAttachmentService.getByAttachmentId(attachmentId);
		
//		QualityAttachment selectByAttachmentId = qualityAttachmentService.selectByAttachmentId(attachmentId);
//		QualityAttachmentEntity innerData = selectByAttachmentId.getInnerData();
		
		String checkPicturePath = innerData.getFilePath();
		// 图片预览
		UploadPreviewData.previewPdf(checkPicturePath, response);
		return resultBean;
	}
	
}
